package com.jay.io.clazz;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class InputStreamReaderWriterCls {
	public static void main(String[] args) {
		try {
			File file = new File("code.txt");

			InputStreamReader isr = new InputStreamReader(new FileInputStream(
					file), "GB2312");
			BufferedReader bufInputReader = new BufferedReader(isr);

			System.out.println(isr.getEncoding());
			char[] array = new char[1];
			String text = "";
			while (bufInputReader.read(array) != -1) {
				text += array[0];
			}
			System.out.println(text);
			bufInputReader.close();

			System.out.println();

			OutputStreamWriter osr = new OutputStreamWriter(
					new FileOutputStream(file), "GB2312");
			BufferedWriter bufWriter = new BufferedWriter(osr);
			text += "xx中国";
			bufWriter.write(text);

			bufWriter.flush();
			bufWriter.close();
		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("请指定文件名！");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
